package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.UserAnnounceBo;
import com.tpyule.tpclass.entity.dto.AnnounceQueryDto;
import com.tpyule.tpclass.entity.pojo.Notify;
import com.tpyule.tpclass.entity.pojo.UserNotify;
import com.tpyule.tpclass.service.NotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/22 14:19
 */
@RestController
@RequestMapping("/notify")
public class NotifyController extends BaseController{
    @Autowired
    NotifyService notifyService;

    @RequestMapping("/addAnnounce")
    @ResponseBody
    public void addAnnounce(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String annStr = request.getParameter("notifyData");
            Notify notify = objectMapper.readValue(annStr, Notify.class);
            int result = notifyService.createAnnounce(notify);
            if(result>0){
                resultBean = new SuccessResultBean("成功");
            }else{
                resultBean = new FailResultBean("失败");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getSizeAnnounce")
    @ResponseBody
    public void getSizeAnnounce(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            int result = notifyService.getSizeAnnounce();
            if(result>0){
                resultBean = new SuccessResultBean(result,"成功");
            }else{
                resultBean = new FailResultBean("失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/getListAnnounce")
    @ResponseBody
    public void getListAnnounce(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String annStr = request.getParameter("announceQueryData");
            AnnounceQueryDto announceQueryDto = objectMapper.readValue(annStr, AnnounceQueryDto.class);
            if (announceQueryDto == null) {
                resultBean = new FailResultBean("失败！");
            }else {
                List<Notify> notifyList = notifyService.getListAnnounce(announceQueryDto);
                if(notifyList!=null){
                    resultBean = new SuccessResultBean(notifyList,"成功");
                }else{
                    resultBean = new FailResultBean("失败");
                }
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getUserAnnounce")
    @ResponseBody
    public void getUserAnnounce(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String userId = request.getParameter("userId");
            if (userId == null) {
                resultBean = new FailResultBean("失败！");
            }else {
                List<UserAnnounceBo> bos = notifyService.getListAnnounce(userId);
                if(bos!=null){
                    resultBean = new SuccessResultBean(bos,"成功");
                }else{
                    resultBean = new FailResultBean("失败");
                }
            }
        } catch (Exception e) {
            resultBean = new FailResultBean("系统异常");
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getNoReadCount")
    @ResponseBody
    public void getNoReadCount(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String userId = request.getParameter("userId");
            if (userId == null) {
                resultBean = new FailResultBean("失败！");
            }else {
                int result = notifyService.getNoReadCount(userId);
                resultBean = new SuccessResultBean(result,"成功");
            }
        } catch (Exception e) {
            resultBean = new FailResultBean("系统异常");
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/updateNoRead")
    @ResponseBody
    public void updateNoRead(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String userNotifyData = request.getParameter("userNotifyData");
            UserNotify userNotify = objectMapper.readValue(userNotifyData, UserNotify.class);
            if (userNotify == null) {
                resultBean = new FailResultBean("失败！");
            }else {
                int result = notifyService.updateNoRead(userNotify);
                if (result!=0)
                    resultBean = new SuccessResultBean(result,"成功");
                else
                    resultBean = new FailResultBean("失败");
            }
        } catch (Exception e) {
            resultBean = new FailResultBean("系统异常");
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/updateAllNoRead")
    @ResponseBody
    public void updateAllNoRead(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String userId = request.getParameter("userId");
            if (userId == null) {
                resultBean = new FailResultBean("失败！");
            }else {
                int result = notifyService.updateAllNoRead(userId);
                if (result!=0)
                 resultBean = new SuccessResultBean(result,"成功");
                else
                    resultBean = new FailResultBean("失败");
            }
        } catch (Exception e) {
            resultBean = new FailResultBean("系统异常");
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
}
